package org.sbbs.app.gen.webapp.action;

import java.util.StringTokenizer;
import javax.servlet.http.HttpServletRequest;
import org.sbbs.app.gen.model.DetailEntity;
import org.sbbs.app.gen.service.DetailEntityManager;
import org.sbbs.base.webapp.action.BaseJqgridEditAction;

public class DetailEntityEditAction extends BaseJqgridEditAction {	

	private DetailEntity			model;
	private DetailEntityManager	detailEntityManager;
	
	public DetailEntity getModel() {
		return model;
	}
	
	public void setModel(DetailEntity model) {
		this.model = model;
	}
	
	public DetailEntityManager getDetailEntityManager() {
		return detailEntityManager;
	}
	
	public void setDetailEntityManager(DetailEntityManager detailEntityManager) {
		this.detailEntityManager = detailEntityManager;
	}
	
	public String edit() {
		if (getModelId() != null) {
			this.setModel(this.detailEntityManager.get(Long.parseLong(this.getModelId())));
			this.setEditType(EDIT_TYPE_EDIT);
		} else {
			this.setModel(new DetailEntity());
			this.setEditType(EDIT_TYPE_NEW);
		}
		return this.SUCCESS;
		
	}
	
	public String save() {
		try {
			HttpServletRequest req = this.getRequest();
			boolean isNew = (this.getEditType() == 1);
			this.getDetailEntityManager().save((DetailEntity) model);
			this.setMessage(getText((isNew) ? "detailEntity.added": "detailEntity.updated", "no msg key found,save successed."));
		} catch (Exception e) {
			this.setMessage(getText("error.saved",	new String[] { e.getMessage() }));
			this.setReturnCode(RETURN_STATUS_ERROR);
		}
		return this.SUCCESS;
	}
	
	public String delete() {
		try {
			
			StringTokenizer ids = new StringTokenizer(this.getDelIds(), ",");
			while (ids.hasMoreTokens()) {
				Long removeId = Long.parseLong(ids.nextToken());
				this.getDetailEntityManager().remove(removeId);
			}
			this.setMessage(getText("detailEntity.deleted",	"no msg key found,delete successed."));
		} catch (Exception e) {
			this.setMessage(getText("error.deleted",new String[] { e.getMessage() }));
			this.setReturnCode(RETURN_STATUS_ERROR);
		}
		return this.SUCCESS;
	}
	
}